package lv;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class VerItems extends HttpServlet {
	
	
	private static final long serialVersionUID = 1L;

	
    public VerItems() {
        super();
       }

	@SuppressWarnings("unchecked")
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	   
		HttpSession session = request.getSession();
	    
		Set<String> items = (Set<String>)session.getAttribute("Items");
	    
		if (items == null) {
	      items = new HashSet<String>();
	      session.setAttribute("Items", items);
	    }
		
	    String nuevoItem = request.getParameter("nuevoItem");
	    
	    if ((nuevoItem != null) &&
	        (!nuevoItem.trim().equals(""))) {
	      items.add(nuevoItem);
	    }
	    
	    response.setContentType("text/html");
	 
	    
	    PrintWriter out = response.getWriter();
	    String titulo= "Lista de Items";
	    String docType =
	      "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n";
	    out.println(docType +
	                "<HTML>\n" +
	                "<HEAD><TITLE>" + titulo + "</TITLE></HEAD>\n" +
	                "<BODY BGCOLOR=\"#DEDECC\">\n" +
	                "<H1>" + titulo + "</H1>");
	    if (items.size() == 0) {
	      out.println("<I>No hay items</I>");
	    } else {
	      out.println("<UL>");
	      for(String item: items) {
	        out.println("  <LI>" + item);
	      }
	      out.println("</UL>");
	    }
	    out.println("<H3><A href='FormIngreso.html'>retornar</A></H3>");
	    out.println("</BODY></HTML>");

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
